Page({
  data: {
    showsearch: false,   //显示搜索按钮
    searchtext: '',  //搜索文字
    filterdata: {},  //筛选条件数据
    showfilter: false, //是否显示下拉筛选
    showfilterindex: null, //显示哪个筛选类目
    sortindex: 0,  //一级分类索引
    sortid: null,  //一级分类id
    subsortindex: 0, //二级分类索引
    subsortid: null, //二级分类id
    cityindex: 0,  //一级城市索引
    cityid: null,  //一级城市id
    subcityindex: 0,  //二级城市索引
    subcityid: null, //二级城市id
    servicelist: [], //服务集市列表
    scrolltop: null, //滚动位置
    page: 0  //分页
  },
  onLoad: function () { //加载数据渲染页面
    this.fetchServiceData();
    this.fetchFilterData();
  },
  fetchFilterData: function () { //获取筛选条件
    this.setData({
      filterdata: {
        "sort": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "国务院",
            "subsort": [
              {
                "id": 1,
                "title": "全部"
              },
              {
                "id": 11,
                "title": "脱贫困难"
              },
              {
                "id": 12,
                "title": "脱贫攻坚"
              },
            ]
          },
          {
            "id": 2,
            "title": "国家卫生健康委员会",
            "subsort": [
              {
                "id": 2,
                "title": "全部"
              },
              {
                "id": 21,
                "title": "攻坚行动方案"
              },
              {
                "id": 22,
                "title": "大病专项救治"
              },
            ]
          },
          {
            "id": 3,
            "title": "国新办",
            "subsort": [
              {
                "id": 3,
                "title": "全部"
              },
              {
                "id": 31,
                "title": "中国减贫发布会"
              },
              {
                "id": 32,
                "title": "打赢攻坚战的意见指导"
              },
              {
                "id": 33,
                "title": "乡村振兴战略规划"
              }
            ]
          },
          {
            "id": 4,
            "title": "农业农村部",
            "subsort": [
              {
                "id": 4,
                "title": "全部"
              },
              {
                "id": 41,
                "title": "农村集体产权制度改革"
              },
              {
                "id": 42,
                "title": "农村产业融合发展"
              },
              {
                "id": 43,
                "title": "培育新型农业经营主体"
              }
            ]
          },
        ],
        "city": [
          {
            "id": 0,
            "title": "最近"
          },
          {
            "id": 1,
            "title": "半年内"
          },

          {
            "id": 2,
            "title": "一年内",
          },
          {
            "id": 3,
            "title": "一年外",
          },
        ],
      }
    })
  },
  fetchServiceData: function () {  //获取城市列表
    let _this = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    const perpage = 10;
    this.setData({
      page: this.data.page + 1
    })
    const page = this.data.page;
    const newlist = [];
    for (var i = 1; i < 2; i++) {
      newlist.push({
        "id": 1,
        "name": "《乡村振兴战略规划》新闻发布会",
        "city": "一年外",
        "tag": "国新办",
        "imgurl": "http://www.gov.cn/xinwen/2018-09/29/5326689/images/fe2a04e88aad4e8891280eead6274219.jpg"
      },
        {
          "id": 2,
          "name": "农村贫困人口大病专项救治",
          "city": "一年内",
          "tag": "国家卫生健康委员会",
          "imgurl": "https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D220/sign=349cb5129422720e7fcee5f84bca0a3a/96dda144ad3459828fdcf5e200f431adcbef846f.jpg"
        },
        {
          "id": 3,
          "name": "《关于打赢脱贫攻坚战三年行动的指导意见》",
          "city": "最近",
          "tag": "国新办",
          "imgurl": "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2714167126,1049107218&fm=173&app=25&f=JPEG?w=500&h=259&s=23D25F9403616D0B50C9E848030050F2"
        },
        {
          "id": 4,
          "name": "村集体产权制度改革进展情况",
          "city": "半年内",
          "tag": "农业农村部",
          "imgurl": "http://t10.baidu.com/it/u=4143616116,3965568511&fm=173&app=25&f=JPEG?w=600&h=400&s=A202F0044EF598CC17727E910300E08E"
        },
        {
          "id": 5,
          "name": "脱贫攻坚工作有关情况",
          "city": "未最近",
          "tag": "国务院扶贫办",
          "imgurl": "http://images.chinagate.cn/site1020/2018-01/18/61eb44e2-dbae-40e1-82a3-d2db4004db38.jpg"
        })
    }
    setTimeout(() => {
      _this.setData({
        servicelist: _this.data.servicelist.concat(newlist)
      })
    }, 1500)
  },
  inputSearch: function (e) {  //输入搜索文字
    this.setData({
      showsearch: e.detail.cursor > 0,
      searchtext: e.detail.value
    })
  },
  submitSearch: function () {  //提交搜索
    console.log(this.data.searchtext);
    this.fetchServiceData();
  },
  setFilterPanel: function (e) { //展开筛选面板
    const d = this.data;
    const i = e.currentTarget.dataset.findex;
    if (d.showfilterindex == i) {
      this.setData({
        showfilter: false,
        showfilterindex: null
      })
    } else {
      this.setData({
        showfilter: true,
        showfilterindex: i,
      })
    }
    console.log(d.showfilterindex);
  },
  setSortIndex: function (e) { //服务类别一级索引
    const d = this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      sortindex: dataset.sortindex,
      sortid: dataset.sortid,
      subsortindex: d.sortindex == dataset.sortindex ? d.subsortindex : 0
    })
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
  },
  setSubsortIndex: function (e) { //服务类别二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subsortindex: dataset.subsortindex,
      subsortid: dataset.subsortid,
    })
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
  },
  setCityIndex: function (e) { //服务城市一级索引
    const d = this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      cityindex: dataset.cityindex,
      cityid: dataset.cityid,
      subcityindex: d.cityindex == dataset.cityindex ? d.subcityindex : 0
    })
    console.log('状态id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
  },
  setSubcityIndex: function (e) { //服务城市二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subcityindex: dataset.subcityindex,
      subcityid: dataset.subcityid,
    })
    console.log('状态id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
  },
  hideFilter: function () { //关闭筛选面板
    this.setData({
      showfilter: false,
      showfilterindex: null
    })
  },
  scrollHandle: function (e) { //滚动事件
    this.setData({
      scrolltop: e.detail.scrollTop
    })
  },
  goToTop: function () { //回到顶部
    this.setData({
      scrolltop: 0
    })
  },
  //scrollLoading:function(){ //滚动加载
  // this.fetchServiceData();
  // },
  onPullDownRefresh: function () { //下拉刷新
    this.setData({
      page: 0,
      servicelist: []
    })
    this.fetchServiceData();
    this.fetchFilterData();
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})